<?php
class BrocantesController extends Controller{
	function index(){
		$perPage = 5;

		//Récupération des catégories
		$this->loadModel('Categorie');
		$d['categories'] = $this->Categorie->find(array(
			'fields' 	 => 'id, nom',
			));

		$this->loadModel('Brocante');
		$condition = array('online' => 1,'type'=>'broc');

		//Filtre categBrocante
		if(isset($_GET['categBrocante'])){
			if((!empty($_GET['categBrocante'])) && ($_GET['categBrocante'] != "categ_toutes")){
				$condition['idCategorie'] = $_GET['categBrocante'];
			}
		}

		//Filtre dateBrocante
		if(isset($_GET['dateBrocante'])){
			if(!empty($_GET['dateBrocante'])){
				$dateB = str_replace('/', '-', $_GET['dateBrocante']);
				$condition['cast( dateBroc as date)']=strftime('%Y-%m-%d %H:%S', strtotime($dateB));
			}
		}

		//Filtre etatBrocante
		if(isset($_GET['etatBrocante'])){
			if(!empty($_GET['etatBrocante'])){
				if($_GET['etatBrocante'] =="passe"){
					$d['brocantes'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'orderd' => 'dateBroc',
						'dateBrocInf' => (new DateTime())->format('Y-m-d'),
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));	
				}
				else if($_GET['etatBrocante'] =="cours"){
					$condition['cast(dateBroc as date)']= (new DateTime())->format('Y-m-d');
					$d['brocantes'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'orderd' => 'dateBroc',
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));						
				}
				else if($_GET['etatBrocante'] =="venir"){
					$dateSup = new DateTime();
					$dateSup->modify('+1 day');
					$d['depts'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'dateBrocSup' => $dateSup->format('Y-m-d') ,
						'orderd' => 'dateBroc',
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));		
				}
				else{
					$d['brocantes'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'orderd' => 'dateBroc',
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));
				} 
			}
			else{
				$d['brocantes'] = $this->Brocante->find(array(
					'conditions' => $condition,
					'orderd' => 'dateBroc',
					'limit' => ($perPage*($this->request->page-1)).','.$perPage
					)); 
			}
		}else{
			$d['brocantes'] = $this->Brocante->find(array(
				'conditions' => $condition,
				'orderd' => 'dateBroc',
				'limit' => ($perPage*($this->request->page-1)).','.$perPage
				));
		};

			//debug($condition);
			// $d['brocantes'] = $this->Brocante->find(array(
			// 	'conditions' => $condition,
			// 	'order' => 'dateBroc',
			// 	'limit' => ($perPage*($this->request->page-1)).','.$perPage
			// 		));
		$d['total'] = $this->Brocante->findCount($condition);
		$d['page'] = ceil($d['total'] / $perPage);
		$this->set($d);
	}



	function view($id,$slug){
		$this->loadModel('Brocante');
		
		$d['brocantes'] = $this->Brocante->findFirst( array(
			'fields' 	 => 'id,slug,description,idCategorie,lieu,nom, xLat, xLong, dateBroc, numDepartement',
			'conditions' => array('online' => 1,'id'=>$id,'type'=>'broc') 
			));

		//On récupère la catégorie
		$this->loadModel('Categorie');
		$d['categorie'] = $this->Categorie->findFirst(array(
			'fields' 	 => 'id, nom',
			'conditions' => array('id' => $d['brocantes']->idCategorie),
			));
		
		if(empty($d['brocantes'])){
			$this->e404('Page introuvable');
		}
		if ($slug != $d['brocantes']->slug) {
			$this->redirect("brocantes/view/$id/".$d['brocantes']->slug,301);
		}

		$this->set($d);
	}

	function dept($dept){
		//Test si le département à une longueur supérieur à 2
		if(strlen($dept) > 2){
			$this->e404('Page introuvable');
		}

		$perPage = 5;

		//Récupération des catégories
		$this->loadModel('Categorie');
		
		$d['categories'] = $this->Categorie->find(array(
			'fields' 	 => 'id, nom',
			));

		$this->loadModel('Brocante');
		$d['dept'] = $dept;
		$condition = array('online' => 1,'type'=>'broc','numDepartement' => $dept);

//Filtre categBrocante
		if(isset($_GET['categBrocante'])){
			if((!empty($_GET['categBrocante'])) && ($_GET['categBrocante'] != "categ_toutes")){
				$condition['idCategorie'] = $_GET['categBrocante'];
			}
		}
		
			//Filtre
		if(isset($_GET['dateBrocante'])){
			if(!empty($_GET['dateBrocante'])){
				$dateB = str_replace('/', '-', $_GET['dateBrocante']);
				$condition['cast( dateBroc as date)']=strftime('%Y-%m-%d %H:%S', strtotime($dateB));
			}
		}

			 //Filtre etatBrocante
		if(isset($_GET['etatBrocante'])){
			if(!empty($_GET['etatBrocante'])){
				if($_GET['etatBrocante'] =="passe"){
					$d['depts'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'orderd' => 'dateBroc',
						'dateBrocInf' => (new DateTime())->format('Y-m-d'),
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));	
				}
				else if($_GET['etatBrocante'] =="cours"){
					$condition['cast(dateBroc as date)']= (new DateTime())->format('Y-m-d');
					$d['depts'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'orderd' => 'dateBroc',
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));						
				}
				else if($_GET['etatBrocante'] =="venir"){
					$dateSup = new DateTime();
					$dateSup->modify('+1 day');
					$d['depts'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'dateBrocSup' => $dateSup->format('Y-m-d') ,
						'orderd' => 'dateBroc',
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));	
				}
				else{
					$d['depts'] = $this->Brocante->find(array(
						'conditions' => $condition,
						'orderd' => 'dateBroc',
						'limit' => ($perPage*($this->request->page-1)).','.$perPage
						));
				} 
			}
			else{
				$d['depts'] = $this->Brocante->find(array(
					'conditions' => $condition,
					'orderd' => 'dateBroc',
					'limit' => ($perPage*($this->request->page-1)).','.$perPage
					)); 
			}
		}else{
			$d['depts'] = $this->Brocante->find(array(
				'conditions' => $condition,
				'orderd' => 'dateBroc',
				'limit' => ($perPage*($this->request->page-1)).','.$perPage
				));
		};
			//debug($condition);
			// $d['depts'] = $this->Brocante->find(array(
			// 	'conditions' => $condition,
			// 	'limit' => ($perPage*($this->request->page-1)).','.$perPage
			// 		));
		$d['total'] = $this->Brocante->findCount($condition);
		$d['page'] = ceil($d['total'] / $perPage);
		$this->set($d);
	}

	function geo($xLat, $xLong, $rayon){
		$perPage = 5;
		$this->loadModel('Brocante');
		$condition = array('cast(dateBroc as date)' => date('Y-m-d'));
			$rayon = $rayon*1000; //10 km
			$d['brocantes'] = $this->Brocante->find(array(
				'conditions' => $condition,
				'fields' 	=> '*,get_distance_metres(\''.$xLat.'\', \''.$xLong.'\', xLat,xLong) AS proximite',
				'limit' 	=> ($perPage*($this->request->page-1)).','.$perPage,
				'order'		=> 'proximite',
				'having'	=> 'proximite < '.$rayon
				));
			$d['total'] = $this->Brocante->findCount($condition);
			$d['page'] = ceil($d['total'] / $perPage);
			$this->set($d);
		}

	/**
	* ADMIN
	**/
	function admin_index(){
		$perPage = 10;
		$this->loadModel('Brocante'); 
		$condition = array('');
		$d['actus'] = $this->Brocante->find(array(
			'fields' 	 => 'id,nom,online,numDepartement',
			'conditions' => $condition,
			'limit' 	 => ($perPage*($this->request->page-1)).','.$perPage,
			'orderd'	 => 'dateBroc',
			));
		$d['total'] = $this->Brocante->findCount($condition);
		$d['page'] = ceil($d['total'] / $perPage);
		$this->set($d);
	}

	/**
	* Permet d'éditer une actu
	**/
	function admin_edit($id = null){
		//Récupération des départements
		$this->loadModel('Departement');
		$d['departements'] = $this->Departement->find(array(
			'fields' 	 => 'numDepartement, nom',
			'orderlpad'	 => 'numDepartement',
			));
		//Récupération des catégories
		$this->loadModel('Categorie');
		$d['categories'] = $this->Categorie->find(array(
			'fields' 	 => 'id, nom',
			));

		$this->loadModel('Brocante');
		$d['id']= '';
		//debug($this->request->data);
		if ($this->request->data){
			$this->request->data->dateAjout = date('Y-m-d h:i:s');
			$this->request->data->type = 'broc';

			$this->request->data->dateBroc = str_replace('/', '-', $this->request->data->dateBroc);

			//conversion de dateBroc
			if (isset($this->request->data->dateBroc)){
			//$this->request->data->dateBroc = date('Y-d-m H:i', strtotime($this->request->data->dateBroc));

				$this->request->data->dateBroc = strftime('%Y-%m-%d %H:%S', strtotime($this->request->data->dateBroc));
			}

			 //enregistrement de l'image de l'input file
			if($this->request->data && !empty($_FILES['fichier']['name'])){
				if(strpos($_FILES['fichier']['type'], 'image') !== false){
					$dir = WEBROOT.DS.'img/phBroc/';
					$file = $_FILES['fichier']['tmp_name'];
					move_uploaded_file($file,$dir.DS.$id.'.jpg');
					$this->Session->setFlash("Le fichier a été correctement uploadé",'alert-success');
				}else{
					$this->Form->errors['file'] = "Le fichier n'est pas une image";
				}
			}

			$this->Brocante->save($this->request->data);
			$this->Session->setFlash('Le contenu a bien été modifié','alert-success');
			$id = $this->Brocante->id;
			$this->redirect('admin/brocantes/index');	
		}
		if($id){
			$this->request->data = $this->Brocante->findFirst(array(
				'conditions' => array('id'=>$id)
				));
			$d['id'] = $id;
		}
		$this->set($d);
	}

	/**
	* Permet de supprimer une actu
	**/
	function admin_delete($id){
		$this->loadModel('Brocante');
		$this->Brocante->delete($id);
		$this->Session->setFlash('Le contenu a bien été supprimé','alert-success');
		$this->redirect('admin/brocantes/index');
	}


}
?>